using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model;
using ZR.Model.System.Dto;
using ZR.ServiceCore.Services.IService;

namespace ZR.Admin.WebApi.Controllers.System
{
    /// <summary>
    /// 表查询管理
    /// </summary>
    [Verify]
    [Route("system/tableQuery")]
    [ApiExplorerSettings(GroupName = "sys")]
    public class TableQueryController : BaseController
    {
        /// <summary>
        /// 表查询服务
        /// </summary>
        private readonly ITableQueryService TableQueryService;

        public TableQueryController(ITableQueryService tableQueryService)
        {
            TableQueryService = tableQueryService;
        }

        /// <summary>
        /// 查询包含Z_RADR_I的表列表
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("list")]
        public IActionResult List([FromBody] TableQueryListRequest request)
        {
            var query = new TableQueryDto
            {
                Type = request.Type,
                BeginTime = request.BeginTime,
                EndTime = request.EndTime,
                City = request.City
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = TableQueryService.SelectTableList(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 获取表查询统计信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("statistics")]
        [ActionPermissionFilter(Permission = "system:tableQuery:list")]
        public IActionResult GetStatistics()
        {
            var statistics = TableQueryService.GetTableQueryStatistics();
            return SUCCESS(statistics);
        }

        /// <summary>
        /// 查询PPI表列表（所有Z_RADR_I表名，不包含LR_096）
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("ppi/list")]
        [ActionPermissionFilter(Permission = "system:tableQuery:list")]
        public IActionResult GetPPITables([FromBody] TableQueryListRequest request)
        {
            var query = new TableQueryDto
            {
                Type = "PPI"
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = TableQueryService.SelectTableList(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 查询DBS表列表（所有Z_RADR_I表名且包含有LR_096）
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("dbs/list")]
        [ActionPermissionFilter(Permission = "system:tableQuery:list")]
        public IActionResult GetDBSTables([FromBody] TableQueryListRequest request)
        {
            var query = new TableQueryDto
            {
                Type = "DBS"
            };
            var pager = new PagerInfo
            {
                PageNum = request.PageNum,
                PageSize = request.PageSize
            };
            var list = TableQueryService.SelectTableList(query, pager);
            return SUCCESS(list);
        }

        /// <summary>
        /// 导出表查询结果
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns></returns>
        [HttpPost("export")]
        [ActionPermissionFilter(Permission = "system:tableQuery:export")]
        [Log(Title = "表查询", BusinessType = BusinessType.EXPORT)]
        public IActionResult Export([FromBody] TableQueryListRequest request)
        {
            var query = new TableQueryDto
            {
                Type = request.Type
            };
            var pager = new PagerInfo
            {
                PageNum = 1,
                PageSize = 10000 // 导出时限制最大数量
            };
            var list = TableQueryService.SelectTableList(query, pager);
            var result = ExportExcelMini(list.Result, "tableQuery", "表查询结果");
            return ExportExcel(result.Item2, result.Item1);
        }
    }
}
